package com.pg_chat.controller;


import com.pg_chat.model.ChatRoom;
import com.pg_chat.model.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;


@ServerEndpoint("chat/talkRoom/{sender}/{receiver}/{groupId}")
@RequestMapping
public class ChatRoomController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //存储每个会话对象
    private static ConcurrentHashMap<String, ChatRoom> room = new ConcurrentHashMap<>();
    private Session session;
    private UserEntity user;
    //会话存储对象 和 朋友对象
    private Object talkRoom;
    private Object Friends;


    @Autowired
    private SimpMessagingTemplate template;

    @Scheduled(fixedRate = 10000)
    public void sendQueueMessage() {
        System.out.println("后台一对一推送！");
        UserEntity user = new UserEntity();
        user.setUserId(1);
        user.setUsername("oyzc");
        user.setAge((short) 10);
        this.template.convertAndSendToUser(user.getUserId() + "", "/queue/getResponse", user);
    }

    /**
     * 接入聊天
     */
    @OnOpen
    public void onOpen(@PathParam("sender")String sender,
                       @PathParam("receiver")String receiver,
                       @PathParam("groupId")String groupId,
                       Session session){
        //存储对象
        ChatRoom chatRoom = new ChatRoom();
        chatRoom.setUserId(sender);
        chatRoom.setSession(session);
        //将会话信息存储
        room.put(sender,chatRoom);


    }

}


